#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/1/23 20:02
# @Author  : Cxk

from PyQt5.QtWidgets import QApplication, QMainWindow,QGridLayout,QFileDialog,QMessageBox
from PyQt5 import QtCore, QtGui, QtWidgets, QtMultimedia
from PyQt5.QtCore import QUrl, Qt
from PyQt5 import QtGui
import sys
import os,random

from get_img_list import get_img_list

global all_img
def GetNameByEveryDir(file_dir,videoProperty):  
        FileNameWithPath = [] 
        FileName         = []
        FileDir          = []
        for root, dirs, files in os.walk(file_dir):  
            for file in files:  
                if os.path.splitext(file)[1] in videoProperty:  
                    FileNameWithPath.append(os.path.join(root, file))  # 保存图片路径
                    FileName.append(file)                              # 保存图片名称
                    FileDir.append(root[len(file_dir):])               # 保存图片所在文件夹
        return FileName,FileNameWithPath,FileDir

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1047, 813)
        MainWindow.setWindowIcon(QtGui.QIcon("mr/logo.ico"))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.count=0 #当前浏览图片的下标
        self.img_number=50 #相似图片总量 大概跟你每个类别下载的图片数量相匹配
        self.button_=False #上下页按钮判断标志
        
        # (y,x,图片长，图片宽)
        self.pushButton_1 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_1.setGeometry(QtCore.QRect(430, 10, 191, 161))
        self.pushButton_1.setObjectName("pushButton_1")
        self.pushButton_1.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}")

        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(20, 190, 191, 161))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_2.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}")
   
        
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(290, 190, 191, 161))
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_3.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}")

        
        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(570, 190, 191, 161))
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_4.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}")
        self.pushButton_4.clicked.connect(lambda:self.setimg("7"))
        
                
        self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_5.setGeometry(QtCore.QRect(830, 190, 191, 161))
        self.pushButton_5.setObjectName("pushButton_5")
        self.pushButton_5.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}")

        self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_6.setGeometry(QtCore.QRect(20, 370, 191, 161))
        self.pushButton_6.setObjectName("pushButton_6")
        self.pushButton_6.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}")

        self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_7.setGeometry(QtCore.QRect(290, 370, 191, 161))
        self.pushButton_7.setObjectName("pushButton_7")
        self.pushButton_7.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}")
        
        self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_8.setGeometry(QtCore.QRect(570, 370, 191, 161))
        self.pushButton_8.setObjectName("pushButton_8")
        self.pushButton_8.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}")
        
        self.pushButton_9 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_9.setGeometry(QtCore.QRect(830, 370, 191, 161))
        self.pushButton_9.setObjectName("pushButton_9")
        self.pushButton_9.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}")
      
        self.pushButton_10 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_10.setGeometry(QtCore.QRect(20, 560, 191, 161))
        self.pushButton_10.setObjectName("pushButton_10")
        self.pushButton_10.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}")

        self.pushButton_11 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_11.setGeometry(QtCore.QRect(290, 560, 191, 161))
        self.pushButton_11.setObjectName("pushButton_11")
        self.pushButton_11.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}")
        
        self.pushButton_12 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_12.setGeometry(QtCore.QRect(570, 560, 191, 161))
        self.pushButton_12.setObjectName("pushButton_12")
        self.pushButton_12.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}")
        
        self.pushButton_13 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_13.setGeometry(QtCore.QRect(830, 560, 191, 161))
        self.pushButton_13.setObjectName("pushButton_13")
        self.pushButton_13.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}")
        
        self.pushButton_17 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_17.setGeometry(QtCore.QRect(350, 750, 101, 41))
        self.pushButton_17.setObjectName("pushButton_17")
        self.pushButton_17.clicked.connect(self.front)

        self.pushButton_18 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_18.setGeometry(QtCore.QRect(470, 750, 101, 41))
        self.pushButton_18.setObjectName("pushButton_18")
        self.pushButton_18.clicked.connect(self.chiose_file)

        self.pushButton_19 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_19.setGeometry(QtCore.QRect(600, 750, 101, 41))
        self.pushButton_19.setObjectName("pushButton_19")
        self.pushButton_19.clicked.connect(self.next)
        
        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        
    def chiose_file(self):
        global all_img
        cwd = os.getcwd()+os.sep+'img' # 获取当前程序文件位置
        fileName_choose, filetype = QFileDialog.getOpenFileName(MainWindow,"选取文件",cwd,"All Files (*);;Text Files (*.txt)")   # 设置文件扩展名过滤,用双分号间隔
        if fileName_choose == "":
            return
        else:
            buttonname="self.pushButton_1"
            buttonname=eval(buttonname)
            buttonname.setToolTip("%s"%fileName_choose.split('/')[-1])
            buttonname.setStyleSheet("QPushButton{border-image: url(%s)}"%fileName_choose)

            # a,b,c=GetNameByEveryDir('./img/','.jpg')
            # all_img=b[0:121]
            #查找的相似图片路径  匹配的相似输数量
            img_number=self.img_number
            all_img=get_img_list(fileName_choose,img_number)
            # print(all_img)
            for i in range(2,14):
                try:
                    buttonname="self.pushButton_%s"%str(i)
                    buttonname=eval(buttonname)
                    buttonname.setToolTip("%s"%all_img[i-2])
                    buttonname.setStyleSheet("QPushButton{border-image: url(img/%s)}"%all_img[i-2])
                except:
                    buttonname="self.pushButton_%s"%str(i)
                    buttonname=eval(buttonname)
                    buttonname.setToolTip("None")
                    buttonname.setStyleSheet("QPushButton{border-image: url(mr/1.jpg)}")
            self.count=12
            self.button_=True

    def front(self):
        global all_img
        if self.button_:
            if self.count>=13:
                self.pushButton_17.setToolTip("上一页")
                for i in range(2,14):
                    buttonname="self.pushButton_%s"%str(i)
                    buttonname=eval(buttonname)
                    buttonname.setToolTip("%s"%all_img[int(self.count)-i-2])
                    buttonname.setStyleSheet("QPushButton{border-image: url(img/%s)}"%all_img[int(self.count)-i-2])
                self.count-=12
            else:
                self.pushButton_17.setToolTip("已经最顶页")
                QMessageBox.information(MainWindow,
                    "提示", 
                    "已经最顶页", 
                    QMessageBox.Yes | QMessageBox.No)

    def next(self):
        global all_img
        if self.button_:
            if self.count<=self.img_number-12:
                self.pushButton_19.setToolTip("下一页")
                for i in range(2,14):
                    buttonname="self.pushButton_%s"%str(i)
                    buttonname=eval(buttonname)
                    buttonname.setToolTip("%s"%all_img[int(self.count)+i-2])
                    buttonname.setStyleSheet("QPushButton{border-image: url(img/%s)}"%all_img[int(self.count)+i-2])
                self.count+=12
            else:
                self.pushButton_19.setToolTip("已经最底页")
                QMessageBox.information(MainWindow,
                    "提示", 
                    "已经最底页", 
                    QMessageBox.Yes | QMessageBox.No)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "以图搜图"))
        self.pushButton_17.setText(_translate("MainWindow", "上一页"))
        self.pushButton_18.setText(_translate("MainWindow", "选择图片"))
        self.pushButton_19.setText(_translate("MainWindow", "下一页"))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
    
